package com.example.landmanagement.util;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

import static com.example.landmanagement.util.Constant.SALT_VALUE;

/**
 * className MD5Tool
 *  MD5加密工具类
 * @author Java DaKun
 * @date 2021/7/14
 * @version:1.0
 * @since:jdk:1.8
 */
public class MD5Tool {
    public static String encodeValue(String value,String encoding){
        Objects.requireNonNull(value);
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            String saltValue =  value+ SALT_VALUE;
            if(encoding==null||"".equals(encoding)){
                messageDigest.update(saltValue.getBytes(Charset.defaultCharset()));
            }else {
                messageDigest.update(saltValue.getBytes(Charset.forName(encoding)));
            }
            byte[] bytes = messageDigest.digest();
            return new BigInteger(1,bytes).toString(16).toUpperCase();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }


    private MD5Tool(){}

    public static void main(String[] args) {
        System.out.println(MD5Tool.encodeValue("1234","utf-8"));
    }
}
